<?php
namespace App\Utilitys;

class BaiduyunOcr
{
    //百度云OCR

    //公司账号
    protected static $appt_id = '114742177 ';
    protected static $client_id = 'UBn2IBODZKMxU8sEPZZ47Mlu';
    protected static $client_secret = 'RrhvSRZkHBRU2ncNKtHOhOWiduWJRBBB';
//    //后端账号
//    protected static $appt_id = '108531653 ';
//    protected static $client_id = 'wiptl4BLTWzBhyaP2ZVSc1aM';
//    protected static $client_secret = 'bke7lECyCgqk2Ti1qkkUN0Cq3fIFUfM5';
//    //前端的账号
//    protected static $appt_id = '114624588 ';
//    protected static $client_id = 't8C6QOLbmb7DsUafEtBFmlpW';
//    protected static $client_secret = 'EZpPWAttt49HdIim7DE4uEoqTtbG8Ft9';


    //获取鉴权
    public static function getAccessToken()
    {
        //https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=&client_secret=;
        $httpUrl = 'https://aip.baidubce.com/oauth/2.0/token';
        $httpUrl .= '?grant_type=client_credentials';
        $httpUrl .= '&client_id=' . self::$client_id;
        $httpUrl .= '&client_secret=' . self::$client_secret;
        $res = self::setCurl($httpUrl, '', 'post');
        if ($res['code'] < 0) {
            return array('code' => -2, 'msg' => $res['msg'], 'data' => $res['data']);
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res['data']);
    }

    //通用文字识别
    public static function getTextFromImg($access_token, $imglink)
    {
        //https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic
        $httpUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic';
        $httpUrl .= '?access_token=' . $access_token;
        $img = file_get_contents($imglink);
        $img = base64_encode($img);
        $bodys = array(
            'url' => $imglink,
        );
        //$bodys = json_encode($bodys, JSON_UNESCAPED_UNICODE);
        $res = self::setCurl($httpUrl, $bodys, 'post');
        if ($res['code'] < 0) {
            return array('code' => -2, 'msg' => $res['msg'], 'data' => $res['data']);
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res['data']);
    }

    /**
     * 构建CURL请求，单线程
     * @param $url 请求地址
     * @param $string 参数拼接的字符串
     * @param $method 请求方式：post、get
     * @return $json
     */
    public static function setCurl($url = '', $string = '', $method = 'get')
    {
        if (!$url) {
            return array('code'=>-2 ,'msg'=>'没有接口地址');
        }
        if (!$method) {
            return array('code'=>-2 ,'msg'=>'没有请求方式');
        }
        $curl = curl_init(); //初始化
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if ($method == 'post') {
            curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
            curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
            curl_setopt($curl, CURLOPT_ENCODING, '');
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
            //curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            //    'Content-Type: application/json', 'Content-Length: ' . strlen($string)
            //));
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        $output = curl_exec($curl);
        curl_close($curl);
        //ddd($output);
        if (!$output) {
            return array('code'=>-1, 'msg'=>'请求失败', 'data' => $output);
        }
        $items = json_decode($output,true);
        return array('code' => 0, 'msg' => 'success', 'data' => $items);
    }
}